/// <reference path="../lib/lib.ts" />
var Game = (function () {
    function Game() {
        if (Game._instance) {
            throw new Error("Error: Singleton");
        }
        this._canvas = document.getElementById("canvas");
        this._world = new b2World(new b2Vec2(0, 0), true);
        this._stage = new Stage(this._canvas);
        var bodyDef = new b2BodyDef();
        bodyDef.type = b2Body.b2_staticBody;
        bodyDef.position.x = 0;
        bodyDef.position.y = 0;
        this._statics = this._world.CreateBody(bodyDef);
        this._statics.SetAwake(false);
        var debugDraw = new b2DebugDraw();
        var canv = document.getElementById("debugCanvas");
        debugDraw.SetSprite(canv.getContext("2d"));
        debugDraw.SetFillAlpha(0.3);
        debugDraw.SetLineThickness(1.0);
        debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
        this._world.SetDebugDraw(debugDraw);
        this._lastUpdateTime = Date.now();
        requestAnimationFrame(this.update);
        Game._instance = this;
    }
    Game.prototype.createStatic = function (fixDef, skin) {
        this._stage.addChild(skin);
        return this._statics.CreateFixture(fixDef);
    };
    Game.prototype.createDynamic = function (bodyDef, skin) {
        this._stage.addChild(skin);
        return this._world.CreateBody(bodyDef);
    };
    Game.prototype.createUnit = function (x, y) {
        this._unit = new Unit(x, y);
    };
    Game.prototype.unitMove = function (dir) {
        this._unit.Move(dir);
    };
    Game.prototype.unitStop = function () {
        this._unit.Stop();
    };
    Game.prototype.startMap = function (mapName) {
        var map = _maps[mapName];
        var blocks = map["PlateBlocks"];
        for (var i = 0; i < blocks.length; i++) {
            var pblock = new PlateBlock(blocks[i][0], blocks[i][1]);
        }
        var blocks = map["BrickBlocks"];
        for (var i = 0; i < blocks.length; i++) {
            var bblock = new BrickBlock(blocks[i][0], blocks[i][1]);
        }
    };
    Game.prototype.update = function () {
        var now = Date.now();
        var dt = now - Game.getInstance()._lastUpdateTime;
        Game.getInstance()._lastUpdateTime = now;
        Game.getInstance()._world.Step(1, 10, 10);
        Game.getInstance()._world.DrawDebugData();
        Game.getInstance()._unit.update();
        Game.getInstance()._stage.update();
        requestAnimationFrame(Game.getInstance().update);
    };
    Game.getInstance = function () {
        if (Game._instance === null) {
            Game._instance = new Game();
        }
        return Game._instance;
    };
    Game._instance = null;
    return Game;
})();
